package com.engine.ch.action;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.engine.addons.common.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


public class SyncBorrowReturnAction implements Action {
    private static final Log log = LogFactory.getLog(SyncBorrowReturnAction.class);
    @Override
    public String execute(RequestInfo requestInfo) {
        log.info(requestInfo.getRequestid());
        JSONObject sendJson = new JSONObject();
        sendJson.put("username", "oa");  // 用户名
        sendJson.put("password", "9pXAbpSBfXHvrcv");  // 密码
        sendJson.put("requestId",requestInfo.getRequestid()); // 流程实例ID
        sendJson.put("status", -1); // 审核状态 1:已审批；2：审批不通过；-1退回
        sendJson.put("remark", "备注");
        log.info("借阅退回请求:"+ sendJson.toString());
        String url ="http://172.30.136.201:8080/szda/api/ext/oa/callback";
        String body = HttpRequest.post(url).header("Content-Type", "application/json").body(sendJson.toString()).execute().body();
        log.info("借阅退回返回:"+ body);
        if (StringUtil.isNull(body)) {
            log.info("同步失败，返回为空");
            return Action.FAILURE_AND_CONTINUE;
        }

        JSONObject resJson = JSONObject.parseObject(body);
        if (resJson.getInteger("code") == -1) {
            log.info("同步失败，返回code不为0");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
